package extend_widget

import (
	"clientDemo/ui/fyne_ui/widget/interface"
	utils2 "clientDemo/ui/fyne_ui/widget/utils"
	"clientDemo/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	widget2 "fyne.io/fyne/v2/widget"
)

type Container struct {
	widgetcommon.IUserInfo
	name string
}

func (c *Container) GetUserInfo() widgetcommon.IUserInfo {
	return c.IUserInfo
}
func (c *Container) SetName(name string) {
	c.name = name
}
func (c *Container) IsContainer() bool {
	return false
}
func (c *Container) SetObjects(objects []fyne.CanvasObject) {
}
func (c *Container) AddWidgetChildObj(objects widgetcommon.IWidgetItem) {
}
func (c *Container) AddObjects(objects fyne.CanvasObject) {
}
func (c *Container) SetPosition(objects fyne.CanvasObject, pos fyne.Position) {
}
func (c *Container) SetTouchBeganCallback(cb widgetcommon.TouchCallback) {
}
func (c *Container) SetTouchMovedCallback(cb widgetcommon.TouchCallback) {
}
func (c *Container) SetTouchEndedCallback(cb widgetcommon.TouchCallback) {
}
func (c *Container) AddWidgetChild(objects widgetcommon.IWidgetItem, pos fyne.Position) {
}
func (c *Container) AddWidgetContainer(objects widgetcommon.IWidgetItem, pos fyne.Position) {
}
func (c *Container) GetBaseSize() fyne.Size {
	return fyne.NewSize(0, 0)
}
func (c *Container) SetParent(parent widgetcommon.IWidgetItem) {}

type WidgetContainer struct {
	widget2.BaseWidget
	positions map[fyne.CanvasObject]fyne.Position
	Border    *canvas.Rectangle
	objects   []fyne.CanvasObject
	*Container
	ac *Action
}

// ForEach s
func (w *WidgetContainer) ForEach(cb func(objects widgetcommon.IWidgetItem) bool) {
	for i := 0; i < len(w.objects); i++ {
		v := w.objects[i]
		o, ok := v.(widgetcommon.IWidgetItem)
		if !ok {
			continue
		}
		if cb(o) {
			return
		}
	}
}

// SetPosition 设置对象的位置
// SetPosition 方法用于设置容器中对象的坐标
func (w *WidgetContainer) SetPosition(objects fyne.CanvasObject, pos fyne.Position) {
	// 将对象的坐标存储到 positions 字典中
	w.positions[objects] = pos
}

func (w *WidgetContainer) SetProperty(key, value string, isProperty bool) {
	w.IUserInfo.SetProperty(key, value, isProperty)
	if key == "Pos" {
		w.ac.GetBase().(fyne.CanvasObject).Move(utils2.ParsePosition(value))
	}
}

// CreateRenderer 实现自定义渲染器
func (w *WidgetContainer) CreateRenderer() fyne.WidgetRenderer {
	o := &customRenderer{
		border:           w.Border,
		positions:        &w.positions,
		objectsInterface: &w.objects,
		name:             w.name,
	}
	return o
}

func (w *WidgetContainer) Move(pos fyne.Position) {
	w.BaseWidget.Move(pos) //只移动地板，其他元素是相对地板布局的
	w.Refresh()            // 确保刷新更新显示
}
func (w *WidgetContainer) Move2(pos fyne.Position) {
	if w.ac != nil && w.ac.GetParent() != nil {
		if w.ac.GetBase().(widgetcommon.IWidgetItem).IsContainer() {
			w.ac.GetBase().(fyne.CanvasObject).Move(pos)
		} else {
			w.BaseWidget.Move(pos)
		}
		w.ac.GetParent().SetPosition(w.ac.GetBase(), pos)
	}
	w.IUserInfo.SetProperty("Pos", utils.ToString(pos.X)+" "+utils.ToString(pos.Y), true)
	w.Refresh() // 确保刷新更新显示
}
func (w *WidgetContainer) SetObjects(objects []fyne.CanvasObject) {
	w.objects = objects
}
func (w *WidgetContainer) SetObjectsInterface(objects []fyne.CanvasObject) {
	w.objects = objects
}

// SetMinSize 保留函数 暂时为空
func (w *WidgetContainer) SetMinSize(size fyne.Size) {
}

// 自定义渲染器
type customRenderer struct {
	border           *canvas.Rectangle
	positions        *map[fyne.CanvasObject]fyne.Position
	objectsInterface *[]fyne.CanvasObject //WidgetContainer对指针象
	name             string
}

func (r *customRenderer) Layout(size fyne.Size) {
	if r.objectsInterface != nil {
		objList := *r.objectsInterface
		for i := 0; i < len(objList); i++ {
			obj := (objList[i])
			if obj == nil {
				continue
			}
			has, pos := r.GetPosition(obj)
			if has {
				sz := obj.MinSize()
				obj.Resize(sz)
				obj.Move(pos)
			} else {
				obj.Resize(size)
				obj.Move(fyne.NewPos(0, 0))
			}
		}
	}
	r.border.Resize(size)
	r.border.Move(fyne.NewPos(0, 0))
}
func (r *customRenderer) MinSize() fyne.Size {
	return fyne.NewSize(0, 0)
}
func (r *customRenderer) Refresh() {
	if r.objectsInterface != nil {
		objList := *r.objectsInterface
		for _, obj := range objList {
			if obj == nil {
				continue
			}
			sz := obj.MinSize()
			obj.Resize(sz)
			obj.Refresh()

			//_image, ok := obj.(*canvas.Image)
			//if ok {
			//	log.Printf("================================[File]MouseMoved %v ", _image.File)
			//}
		}
	}
}
func (r *customRenderer) Destroy() {
}
func (r *customRenderer) Objects() []fyne.CanvasObject {
	var objects []fyne.CanvasObject
	objects = append(objects, r.border)

	if r.objectsInterface != nil {
		objList := *r.objectsInterface
		for i := 0; i < len(objList); i++ {
			obj := (objList[i])
			if obj == nil {
				continue
			}
			objects = append(objects, obj)
			//_image, ok := obj.(*canvas.Image)
			//if ok {
			//	log.Printf("------------------------------[File]MouseMoved %v ", _image.File)
			//}
		}
	}
	return objects
}

// 容器类型的接口
func (r *customRenderer) GetPosition(obj fyne.CanvasObject) (bool, fyne.Position) {
	if r.positions == nil {
		return false, fyne.NewPos(0, 0)
	}
	mapObj := *r.positions
	if mapObj == nil {
		return false, fyne.NewPos(0, 0)
	}
	return true, mapObj[obj]
}

type BaseContainer struct {
	Container
}
